package com.barunsw.bwf.ems.neoarena.ne.tamms.pl;

import java.io.DataInputStream;

public class PlControl implements PLConstants {
	final int   ERROR_LENGTH = 128;
	java.io.DataInputStream stream = null;

	public PlControl(char[] data) {
		try {
			byte[] newData = new byte[data.length];
			for ( int i = 0; i < data.length; i++ )
				newData[i] = (byte)data[i];

			stream = new java.io.DataInputStream(new java.io.ByteArrayInputStream(newData));

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public M0501_MODE getDisIoPort(){
		M0501_MODE mode = new M0501_MODE();
		byte[] ipAddr = new byte[M0501_MODE.TGCFG_IOPORT_IP_STR];
		try{
			mode.count = stream.readInt();
			mode.reason = stream.readInt();
			for(int i=0; i<mode.count; i++){
				mode.data[i].isExist = stream.readShort();
				mode.data[i].status = stream.readShort();
				mode.data[i].cls = stream.readShort();
				mode.data[i].dummy = stream.readShort();
				stream.read(ipAddr, 0, ipAddr.length);
				mode.data[i].ipAddress = (new String(ipAddr)).trim();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M1300_MODE getDisPkgInf(){
		M1300_MODE mode = new M1300_MODE();
		byte[] fileNameByte = new byte[32];
		try{

			for(int i=0; i<mode.pkgInfo.length; i++){
				mode.pkgInfo[i] = new pkg_info();

				mode.pkgInfo[i].type = stream.readInt();
				mode.pkgInfo[i].side = stream.readInt();
				mode.pkgInfo[i].index = stream.readInt();
				mode.pkgInfo[i].version = stream.readInt();
				mode.pkgInfo[i].makeTime = stream.readInt();
				mode.pkgInfo[i].size = stream.readInt();
				mode.pkgInfo[i].checksum = stream.readInt();

				stream.read(fileNameByte, 0, fileNameByte.length);
				mode.pkgInfo[i].fileName = (new String(fileNameByte)).trim();

				mode.pkgInfo[i].ops = stream.readInt();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M1900_MODE getDisSysConf(){
		M1900_MODE mode = new M1900_MODE();
		try{
			mode.type = stream.readInt();
			mode.reason = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public String getErrorReason()
	{
		byte[] err = new byte[ERROR_LENGTH];
		try
		{
			stream.read(err, 0, err.length);
			return (new String(err)).trim();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public int close()
	{
		try
		{
			stream.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
}

